package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NEW_FTWorkAbortActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvLotQTY)
    TextView tvLotQTY;
    @BindView(R.id.tvOrderNum)
    TextView tvOrderNum;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.tvMainLotQTY)
    TextView tvMainLotQTY;
    @BindView(R.id.tvMainOrderNum)
    TextView tvMainOrderNum;
    @BindView(R.id.tvMainLotState)
    TextView tvMainLotState;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.tvMainStep)
    TextView tvMainStep;
    @BindView(R.id.etComment)
    EditText etComment;
    @BindView(R.id.etMainLot)
    EditText etMainLot;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flLotList)
    FrameLayout flLotList;
    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.ivLotArrow)
    ImageView ivLotArrow;

    private FreeUI_GeneralPop mLotListPop;
    private ArrayList<String> mLotList = new ArrayList<>();
    @Override
    protected int getLayoutResId() {
        return R.layout.new_activity_ft_work_abort;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("测试合批");

//        initEdit(etLot, this::getLotData);

        initEdit(etMainLot, s -> LotUtils.searchLotInfo(NEW_FTWorkAbortActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvMainLotQTY.setText(response.getLOT().getMAINQTY());
                tvMainOrderNum.setText(response.getLINEID());
                tvMainLotState.setText(response.getLOT().getSTATE());
                tvMainStep.setText(response.getLOT().getSTEPNAME());
            }

            @Override
            public void onFail(Throwable t) {

            }
        }));

        initEdit(etEqpID,s ->{
            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    ("正在查询LOT信息", new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(NEW_FTWorkAbortActivity.this, true, getSupportFragmentManager())) {
                        if (response.body().getResponse().getBody().getLOTLIST().size() > 0){
                            for (LotInfoEntity.LotBean lot:response.body().getResponse().getBody().getLOTLIST()){
                                mLotList.add(lot.getLOTID());
                            }
                            tvLot.setText(mLotList.get(0));
                            getLotData(mLotList.get(0));
                        }else {
                            showToast("未查询到批号信息",false);
                        }
                    }
                }
                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);

                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "LOTLISTRUNBYEQP");
            map.addProperty("LOTID", "");
            map.addProperty("LOTALIAS", "");
            map.addProperty("DURABLE", "");
            map.addProperty("EQUIPMENTID", s);
            map.addProperty("STEPNAME", "");
            NetUtils.requestPostNet("CPC.LOTQUERY", "TRANSFERMES", NEW_FTWorkAbortActivity.this, map, callBack);

        });

    }

    private void getLotData(String lotID) {
        LotUtils.searchLotInfo(NEW_FTWorkAbortActivity.this, lotID, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvLotQTY.setText(response.getLOT().getMAINQTY());
                tvOrderNum.setText(response.getLINEID());
                tvLotState.setText(response.getLOT().getSTATE());
                tvStepDesc.setText(response.getLOT().getSTEPDESC());
            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.flLotList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
//                if (getText(etMainLot).equals("")){
//                    showToast("请输入合并主批批号",false);
//                    return;
//                }

                if (tvLot.getText().toString().equals("")){
                    showToast("请选择机台尾数批号",false);
                    return;
                }

                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            showConfirmDialog(NEW_FTWorkAbortActivity.this,"合批成功");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(),"LOTFTABORT");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","DefaultAbort");
                map.addProperty("ACTIONCOMMENT",getText(etComment));
                map.addProperty("MEGERLOTID",getText(etMainLot));
                map.addProperty("EQUIPMENTID",getText(etEqpID));
                map.addProperty("ISCHECKSTEPAUTHORITY",false);
                map.addProperty("MANTISSAFLAG","");
                map.addProperty("REELNUM","");
                List<LotList> lotList = new ArrayList<>();
                lotList.add(new LotList(tvLot.getText().toString()));
                map.add("LOTLIST",new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
                NetUtils.requestPostNet("CPC.LOTFTABORT","TRANSFERMES",NEW_FTWorkAbortActivity.this,map,callBack);
                break;
            case R.id.flLotList:
                mLotListPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flLotList.getWidth(), mLotList, ivLotArrow, (codeS, i) -> {
                    tvLot.setText(codeS);
                    getLotData(codeS);
                });
                FreeApi_PopUtils.setArrowShow(ivLotArrow);
                mLotListPop.showAsDropDown(flLotList, 0, 0);
                break;
        }
    }
}
